﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PMG.model;
using PMG.data;
using PMG.data.Tool;
using System.Text;
using System.IO;

public partial class ADMIN_AccountList : System.Web.UI.Page
{
    #region 注入对象
    private DaoACCOUNT _DaoACCOUNT;
    public DaoACCOUNT DaoACCOUNT
    {
        get { return _DaoACCOUNT; }
        set { _DaoACCOUNT = value; }
    }
    #endregion

    #region 分页参数
    private int pageindex = 1;              // 当前页号
    private int pagesize = 20;              // 每页显示的文章数
    private int pagecount                   // 总页数
    {
        get { return Convert.ToInt32(ViewState["pc"]); }
        set { ViewState["pc"] = value; }
    }
    private int recordcount                 // 总记录数
    {
        get { return Convert.ToInt32(ViewState["rc"]); }
        set { ViewState["rc"] = value; }
    }
    private string where
    {
        get { return (Session["whereinfopas"] == null ? "" : Session["whereinfopas"].ToString()); }
        set { Session["whereinfopas"] = value; }
    }                 // 获取条件
    //列名
    private const string ColumnNames
        = "Id,FULLNAME,Province,City,COMPANY,TEL,CREATEDATE,Questionnaireid";

    #endregion

    #region 数据源
    private DataTable dt
    {
        get { return (DataTable)ViewState["abdt"]; }
        set { ViewState["abdt"] = value; }
    }                 //数据源
    private int rowIndex
    {
        get { return Convert.ToInt32(ViewState["rowindex"]); }
        set { ViewState["rowindex"] = value; }
    }                 //更新的行ID
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        PrepareParam();
        GetRequest();
        GenPager();
        if (!IsPostBack)
        {
            this.dt = DaoACCOUNT.GetPagerByStoreProc(ColumnNames, pagesize, pageindex, "Id", false, where);
            this.gridAlbum.DataSource = dt;

            this.gridAlbum.DataBind();
        }
    }

    #region 生成分页器
    private void GenPager()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("<div class=\"jogger\">");
        if (recordcount > 0 && pagecount > 0 && pageindex > 0)
        {
            // 向前
            if (pageindex == 1)
            {
                sb.AppendLine("<span class=\"disabled\"> << </span>");
                sb.AppendLine("<span class=\"disabled\"> < </span>");
            }
            else
            {
                sb.AppendFormat("<a href=\"AccountList.aspx?p=1\"> << </a>");
                sb.AppendFormat("<a href=\"AccountList.aspx?p={0}\"> < </a>", pageindex - 1);
            }
            // 记录已经显示的页号
            int currMax = 0;
            for (int i = pageindex - 5; i < pageindex + 6; i++)
            {
                // 页号不越界
                if (i > 0 && i <= pagecount)
                {
                    if (i != pageindex)
                        sb.AppendFormat("<a href=\"AccountList.aspx?p={0}\">{0}</a>", i);
                    else
                        sb.AppendFormat("<span class=\"current\">{0}</span>", i);
                    currMax = i;
                }
            }
            // 添加最后的页号
            if (currMax < pagecount)
            {
                sb.AppendLine("...");
                sb.AppendFormat("<a href=\"AccountList.aspx?p={0}\">{0}</a>", pagecount);
            }
            // 向后
            if (pageindex < pagecount)
            {
                sb.AppendFormat("<a href=\"AccountList.aspx?p={0}\"> > </a>", pageindex + 1);
                sb.AppendFormat("<a href=\"AccountList.aspx?p={0}\"> >> </a>", pagecount);
            }
            else
            {
                sb.AppendLine("<span class=\"disabled\"> >> </span>");
                sb.AppendLine("<span class=\"disabled\"> > </span>");
            }
        }
        sb.AppendLine("</div>");
        lt_pager.Text = sb.ToString();
    }
    #endregion

    #region 参数准备
    private void PrepareParam()
    {
        //记录总数
        this.recordcount = DaoACCOUNT.GetRecordCount(this.where);
        this.pagecount = GetPageCount(this.recordcount, this.pagesize);
    }
    #endregion

    #region 获取页数
    public int GetPageCount(int recordCount, int pageSize)
    {
        int pagecount = recordCount / pageSize;
        // 余数
        int yushu = recordCount % pageSize;
        if (yushu > 0)
            pagecount += 1;
        return pagecount;
    }
    #endregion

    #region 接收页面参数
    private void GetRequest()
    {
        // 页号
        if (Request["p"] != null && Request["p"] != string.Empty)
        {
            try
            {
                this.pageindex = Convert.ToInt32(Request["p"]);
            }
            catch
            {
                //ErrorHandle();
            }
        }
        else
        {
            this.pageindex = 1;
        }
    }
    #endregion

    protected void gridAlbum_PreRender(object sender, EventArgs e)
    {
        if (gridAlbum.Rows.Count > 0)
        {
            this.gridAlbum.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
    }
}
